import React from "react";
import _ from 'lodash';
import { Switch, Route, Redirect,withRouter } from "react-router-dom";
import { loadable } from "@utils/loadable";
//
const Resouce404 = loadable(import('@views/Resouce404.jsx'));
//
const Home = loadable(import('@views/Home.jsx'));
const Login = loadable(import('@views/Login.jsx'));
const Content = loadable(import('@views/common/Content.jsx'));
const List = loadable(import('@views/list/Content.jsx'));
const ListDetail = loadable(import('@views/list/Detail.jsx'));
const Search = loadable(import('@views/list/Search.jsx'));

let initDataLoading = false;
//
@withRouter
class Router extends React.Component{
    initData = ()=>{
        if(initDataLoading){
            return;
        }
        //TODO 初始化数据：挪到store里吧
        Promise.all([
            _.api("page.temp",{params:{pageSize:1000,TXT:"导航栏"}}),// 导航栏
            _.api("page.detail",{params:{slug:"index_footer",TXT:"底部"}}),// 底部页面
            _.api("page.page",{params:{cate:"ywly",index:1,TXT:"业务领域"}})// 业务领域
        ]).then(([res1,res2,res3])=>{
            let list = res1.rows;
            let indexNav = [],businessArr = [];
            list.map((item)=>{
                // nav
                if(item.cate == "index_nav"){
                    indexNav.push({imgPath:item.pic})
                }
                // 导航菜单
                if(item.cate == "index_menu"){
                    try{
                        global.MENU = JSON.parse(item.content)
                    }catch (e) {
                        console.warn("导航栏格式错误")
                    }
                }
            })
            // 业务领域
            res3.rows.map((v)=>{
                businessArr.push({
                    text:v.title,
                    url:`/con/${v.slug}`
                })
            })
            //
            let menuData = global.MENU;
            menuData.business = {
                icon:'/img/menu_icon3.png',
                name:'业务领域',
                key:"business",
                list:businessArr
            }
            global.INDEX_NAV = indexNav;
            //
            let row = res2.data;
            global.FOOTER = decodeURIComponent(row.html);
            //
            initDataLoading = true;
            this.forceUpdate();
        })
    }

    constructor(props){
        super(props);
        _.his = this.props.history;
        this.initData();
    }

    render(){
        if(!initDataLoading){
            return <div className="fl-center" style={{height:"100vh"}}>loading ...</div>;
        }
        return (
            <Switch>
                <Route exact path="/login" component={Login} />
                <Route exact path="/home" component={Home} />
                <Route path="/search" component={Search} />
                <Route path="/con" component={Content} />
                <Route path="/list" component={List} />
                <Route path="/group" component={List} />
                <Route path="/listDetail" component={ListDetail} />
                {/*  */}
                <Route exact path="/resouce404" component={Resouce404} />
                <Redirect exact from="/" to="/home" />
                <Redirect exact from="" to="/home" />
                <Redirect from="*" to="/resouce404" />
            </Switch>
        )
    }
}

export default Router;
